/**
 * Created by Neville Kadwa.
 */
package org.shiftone.cdep.browser.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.TreeListener;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;
import org.shiftone.cdep.browser.client.types.CdepClass;

public class CDepBrowser implements EntryPoint, HistoryListener, WindowResizeListener
{
    private Tree tree;
    private HTML depLabel;

    public void onModuleLoad()
    {
        DockPanel panel = new DockPanel();
        panel.setBorderWidth(2);
        panel.setStyleName("mainBody");
        panel.setHeight("100%");
        panel.setWidth("100%");

        tree = new Tree();
        ScrollPanel treePanel = new ScrollPanel(tree);
        treePanel.setHeight("100%");
        treePanel.setWidth("100%");
        panel.add(treePanel, DockPanel.WEST);
        panel.setCellWidth(treePanel, "50%");

        tree.addTreeListener(new TreeListener() {
            public void onTreeItemSelected(TreeItem treeItem)
            {
                depLabel.setText(treeItem.getText());
            }

            public void onTreeItemStateChanged(TreeItem treeItem)
            {
            }
        });

        depLabel = new HTML();
        depLabel.setHeight("100%");
        depLabel.setWidth("100%");
        panel.add(depLabel, DockPanel.CENTER);
//        panel.setCellWidth(depLabel, "50%");

        depLabel.setText("THIS IS MY LABEL");

        DataServiceAsync dataService = (DataServiceAsync) GWT.create(DataService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) dataService;
        endpoint.setServiceEntryPoint("/dataService");

        AsyncCallback callback = new AsyncCallback()
        {
            public void onSuccess(Object result) {
                CdepClass[] classes = (CdepClass[]) result;
                for (int i = 0; i < classes.length; i++)
                {
                    TreeItem item = new TreeItem(classes[i].getName() + " (" + classes[i].getId() + ")");
                    tree.addItem(item);
                }
            }

            public void onFailure(Throwable caught) {
                tree.addItem("FAILURE: " + caught.getMessage());
            }
        };

        Button b = new Button("Debug");
        b.addClickListener(new ClickListener() {
            public void onClick(Widget widget)
            {
                TreeItem item = tree.getSelectedItem();
                String s = "Tree Style: " + tree.getStyleName();
                if (item != null)
                {
                    s += "\nSelected Style: " + item.getStyleName();
                }
                Window.alert(s);
            }
        });
        panel.add(b, DockPanel.SOUTH);

        dataService.getClassNames(callback);

        Window.addWindowResizeListener(this);
        RootPanel.get().add(panel);
    }

    public void onHistoryChanged(String string)
    {

    }

    public void onWindowResized(int i, int i1)
    {

    }
}
